<?php

namespace App\Http\Requests\Frontend\V10\Financial;

use App\Models\SettingLoan;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class LoanStoreRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {

        return [
            'loan_money'=>['required','integer',function($attribute, $value, $fail) {
                    if($value%100!=0){
                        return $fail('借款金额必须是100的整倍数');
                    }
            },"between:{$this->getMoneyRange()[0]},{$this->getMoneyRange()[1]}"],
            'period'=>['required','integer',Rule::in(config('common.common_period'))],
            'ip'=>'required',
            'device_type'=>['required',Rule::in(['Android','IOS','Web'])],
            'device'=>'required',
            'device_os'=>'required',
            'device_mac'=>'required',
            'device_imei'=>'requiredIf:device_type,Android',
            'device_uuid'=>'requiredIf:device_type,Ios',
            'device_app'=>'required',
            'gps'=>'required',
        ];
    }

    public function messages()
    {
        return [
            'loan_money.between'=>"借款金额必须在{$this->getMoneyRange()[0]}至{$this->getMoneyRange()[1]}的范围内",
            'loan_money.required'=>'借款金额不能为空',
            'loan_money.integer'=>'借款金额异常',
            'period.integer'=>'借款期数异常',
            'period.required'=>'借款期数不能为空',
            'period.in'=>'期数不在可用范围内',
            'ip.required'=>'ip不能为空',
            'device_type.required'=>'设备类型不能为空',
            'device_type.in'=>'设备类型不在可用范围内',
            'device.required'=>'设备型号不能为空',
            'device_os.required'=>'设备系统不能为空',
            'device_mac.required'=>'设备mac地址不能为空',
            'device_imei.requiredIf'=>'设备IMEI不能为空',
            'device_uuid.requiredIf'=>'设备UUID不能为空',
            'device_app.required'=>'设备版本号不能为空',
            'gps.required'=>'gps号不能为空',
        ];
    }

    private function getMoneyRange()
    {
        return json_decode(SettingLoan::first()->loan_range);
    }
}
